<?php
/**
 * 菜单管理类
 * comfiger
 * 2016-8-28
 **/
namespace Mama\Model;
use Think\Model;
class ShopModel extends Model{
	//定义表名称
	protected $tableName = 'shop';
	
	/**
	 * 添加用户信息
	 **/
	public function registerShopInfo($data){
		return $this->data($data)->add();
	}
	
	/**
	 * 根据分店ID获取信息
	 **/
	public function getShopInfoByUsername($shopid){
		return $this->where(array('shop_id' => $shopid))->find();
	}
	
	/**
	 * 根据条件查询总数
	 **/
	public function getShopCount($where){
		return $this->where($where)->count();
	}
	
	/**
	 * 获取用户列表
	 **/
	public function getShopList($where){
		return $this->where($where)->order('shop_id desc')->page($page, C('PAGE_SIZE'))->select();
	}
	
	/**
	 * 获取详情
	 **/
	public function getShopInfoByShopId($shopid){
		return $this->where(array('shop_id' => $shopid))->find();
	}

	/**
	 * 编辑分店信息
	 **/
	public function editShopInfoByShopId($shopid, $data){
		return $this->where(array('shop_id' => $shopid))->setField($data);
	}
	/**
	 * 获取用户管理的分店列表
	 **/
	public function getShoprofileList($userid, $field='*'){
		$list = M('shop_profile')->alias('profile')
				->join('left join mama_shop as shop on profile.shop_id = shop.shop_id')
				->where(array('profile.userid' => $userid))
				->field($field)
				->order('shop.shop_id desc')
				->select();
		//echo M('shop_profile')->_sql();
		return $list;		
	}

	public function getUserListByShopId($shopid){
		return M('shop_profile')->where(array('shop_id' => $shopid))->field('userid')->select();
	}
	
	public function addShopprofileInfo($userid, $shop_id){
		return M('shop_profile')->data(array(
			'userid' => $userid,
			'shop_id' => $shop_id,
			'createtime' => time()
		))->add();
	}
	
	public function addAllShopprofileInfo($data){
		return M('shop_profile')->addAll($data);
	}

	public function deleteAllProfile($userid){
		return M('shop_profile')->where(array(
			'userid' => $userid
		))->delete();
	}
}